package com.mgfireworks.mgplatform;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cn.hn.java.summer.mvc.WebContext;

import com.mgfireworks.mgplatform.mgadmin.constants.Constant;
import com.mgfireworks.mgplatform.mgadmin.user.model.User;

@Component
public class ActionInterceptor extends HandlerInterceptorAdapter {
	
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);	
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		//为上下文准备response
		WebContext.preperedResponse(response);

		String root=request.getContextPath();
		String url=request.getRequestURI();
		//登录检测
		if((
				WebContext.getSessionCookie(Constant.USER_SESSION_KEY,User.class)==null
			) &&
			!url.startsWith(root+"/login.html")
		){
			WebContext.redirectTo("/login.html");
		}
		return super.preHandle(request, response, handler);
	}
}
